import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Font;

public class CommunityChestUI extends JPanel {
	private BoardUi board;
	private JLabel lblChestCard;
	private CommunityCards c = new CommunityCards(1);
	private JButton btnReturn,btnDrawCard;
	private JButton btnPay;
	private int chestPayment;
	private Player p;
	private Block[] bl;

	public CommunityChestUI(Player currentPlayer, CommunityChest communityChest, BoardUi board_, Block[] bl_) {
		this.p = currentPlayer;
		this.board=board_;
		this.bl=bl_;
		
		setBackground(new Color(250, 250, 210));
		setLayout(null);
		
		lblChestCard = new JLabel("");
		lblChestCard.setBounds(0, 52, 200, 113);
		add(lblChestCard);
		
		btnReturn = new JButton("Return");
		btnReturn.setVisible(false);
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				returnGame();
			}
		});
		btnReturn.setBounds(199, 110, 81, 23);
		add(btnReturn);
		
		JLabel lblNewLabel = new JLabel("Community Chest");
		lblNewLabel.setFont(new Font("Verdana", Font.BOLD | Font.ITALIC, 18));
		lblNewLabel.setBounds(0, 11, 188, 35);
		add(lblNewLabel);
		
		btnDrawCard = new JButton("Draw a Card");
		btnDrawCard.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				c.shuffleCommunity();
				justShowsCard();
				btnDrawCard.setVisible(false);
				if(c.getCommunityRandom()!=19 && c.getCommunityRandom()!=20 && c.getCommunityRandom()!=21 && c.getCommunityRandom()!=22 && c.getCommunityRandom()!=23 ){
				if(board.getCurrentPlayer().getInJail()!=true){
					btnReturn.setVisible(true);
				}else{ 
					if(board.getCurrentPlayer().getHasGetOutOfJailCard()<=0){
						JOptionPane.showMessageDialog(null,"You are in Jail na, press Susunod");
						board.getGameButtonUI().enableEndTurnBtn();
						board.getDiceUI().disableRollBtn();
						setVisible(false);
					}else if (board.getCurrentPlayer().getHasGetOutOfJailCard()>0) {
						board.MoveOutOfJail();
						JOptionPane.showMessageDialog(null,"You used JAIL FREE CARD ");
						board.getGameButtonUI().enableEndTurnBtn();
					}
				}
				}
			}
		});
		btnDrawCard.setBounds(47, 166, 108, 23);
		add(btnDrawCard);
		
		btnPay = new JButton("Pay");
		btnPay.setVisible(false);
		btnPay.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Pay();
			}
		});
		btnPay.setBounds(203, 66, 77, 23);
		add(btnPay);
	}
	
	public void justShowsCard() {
		if(c.getCommunityRandom()==1){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest backrizal1.jpg")));
			JOptionPane.showMessageDialog(null,"You Need to Move to Rizal.");
			setVisible(false);
			if(board.currentPlayer.getCurrentPosition()==2){
				board.moveToken(39);
			}else if(board.currentPlayer.getCurrentPosition()==17){
				board.moveToken(24);
			}else if(board.currentPlayer.getCurrentPosition()==33){
				board.moveToken(8);
			}
			
		}else if(c.getCommunityRandom()==2){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest collect1.jpg")));
			board.getCurrentPlayer().addMoney(50);
			board.setCurrentPlayer();
			board.getPlayerPanelUI().updatePlayerMoney();
		
		}else if(c.getCommunityRandom()==3){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest collect2.jpg")));
			board.getCurrentPlayer().addMoney(50);
			board.setCurrentPlayer();
			board.getPlayerPanelUI().updatePlayerMoney();
			
		}else if(c.getCommunityRandom()==4){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest collect3.jpg")));
			board.getCurrentPlayer().addMoney(50);
			board.setCurrentPlayer();
			board.getPlayerPanelUI().updatePlayerMoney();
			
		}else if(c.getCommunityRandom()==5){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest GO1.jpg")));
			JOptionPane.showMessageDialog(null,"You need to go");			
			setVisible(false);
			if(board.currentPlayer.getCurrentPosition()==2){
				board.moveToken(38);
			}else if(board.currentPlayer.getCurrentPosition()==17){
				board.moveToken(23);
			}else if(board.currentPlayer.getCurrentPosition()==33){
				board.moveToken(7);
			}
		}else if(c.getCommunityRandom()==6){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest GO2.jpg")));
			JOptionPane.showMessageDialog(null,"You need to go");			
			setVisible(false);
			if(board.currentPlayer.getCurrentPosition()==2){
				board.moveToken(38);
			}else if(board.currentPlayer.getCurrentPosition()==17){
				board.moveToken(23);
			}else if(board.currentPlayer.getCurrentPosition()==33){
				board.moveToken(7);
			}
		}else if(c.getCommunityRandom()==7){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest GO3.jpg")));
			JOptionPane.showMessageDialog(null,"You need to go");			
			setVisible(false);
			if(board.currentPlayer.getCurrentPosition()==2){
				board.moveToken(38);
			}else if(board.currentPlayer.getCurrentPosition()==17){
				board.moveToken(23);
			}else if(board.currentPlayer.getCurrentPosition()==33){
				board.moveToken(7);
			}
		}else if(c.getCommunityRandom()==8){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest GO4.jpg")));
			setVisible(false);
			if(board.currentPlayer.getCurrentPosition()==2){
				board.moveToken(38);
			}else if(board.currentPlayer.getCurrentPosition()==17){
				board.moveToken(23);
			}else if(board.currentPlayer.getCurrentPosition()==33){
				board.moveToken(7);
			}
		}else if(c.getCommunityRandom()==9){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest go jail1.jpg")));
			JOptionPane.showMessageDialog(null,"You Need to go Jail!");
			board.goingToJail();
		
		}else if(c.getCommunityRandom()==10){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest go jail2.jpg")));
			JOptionPane.showMessageDialog(null,"You Need to go Jail!");
			board.goingToJail();
			board.getGameButtonUI().enableEndTurnBtn();

		}else if(c.getCommunityRandom()==11){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest go jail3.jpg")));
			JOptionPane.showMessageDialog(null,"You Need to go Jail!");
			board.goingToJail();

		}else if(c.getCommunityRandom()==12){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest malling1.jpg")));
			JOptionPane.showMessageDialog(null,"You need to go to Glorietta.");			
			setVisible(false);
			if(board.currentPlayer.getCurrentPosition()==2){
				board.moveToken(14);
			}else if(board.currentPlayer.getCurrentPosition()==17){
				board.moveToken(39);
			}else if(board.currentPlayer.getCurrentPosition()==33){
				board.moveToken(23);
			}
			
		}else if(c.getCommunityRandom()==13){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest malling2.jpg")));	
			JOptionPane.showMessageDialog(null,"You need to go to Moa.");			
			setVisible(false);
			if(board.currentPlayer.getCurrentPosition()==2){
				board.moveToken(16);
			}else if(board.currentPlayer.getCurrentPosition()==17){
				board.moveToken(1);
			}else if(board.currentPlayer.getCurrentPosition()==33){
				board.moveToken(25);
			}
			
		}else if(c.getCommunityRandom()==14){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest mrt1.jpg")));
			if(board.getCurrentPlayer().getCurrentPosition()== 2){
				JOptionPane.showMessageDialog(null,"You need to go to MRT1,It's the closest.");			
				setVisible(false);
				board.moveToken(3);
			}else if(board.getCurrentPlayer().getCurrentPosition()== 17){
				JOptionPane.showMessageDialog(null,"You need to go to MRT3,It's the closest.");			
				setVisible(false);
				board.moveToken(8);
			}else if(board.getCurrentPlayer().getCurrentPosition()== 33){
				JOptionPane.showMessageDialog(null,"You need to go to MRT4,It's the closest.");			
				setVisible(false);
				board.moveToken(2);	
			}
		}else if(c.getCommunityRandom()==15){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest mrt2.jpg")));
			if(board.getCurrentPlayer().getCurrentPosition()== 2){
				JOptionPane.showMessageDialog(null,"You need to go to MRT1,It's the closest.");			
				setVisible(false);
				board.moveToken(3);
			}else if(board.getCurrentPlayer().getCurrentPosition()== 17){
				JOptionPane.showMessageDialog(null,"You need to go to MRT3,It's the closest.");			
				setVisible(false);
				board.moveToken(8);
			}else if(board.getCurrentPlayer().getCurrentPosition()== 33){
				JOptionPane.showMessageDialog(null,"You need to go to MRT4,It's the closest.");			
				setVisible(false);
				board.moveToken(2);	
			}
		}else if(c.getCommunityRandom()==16){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest mrt3.jpg")));
			if(board.getCurrentPlayer().getCurrentPosition()== 2){
				JOptionPane.showMessageDialog(null,"You need to go to MRT1,It's the closest.");			
				setVisible(false);
				board.moveToken(3);
			}else if(board.getCurrentPlayer().getCurrentPosition()== 17){
				JOptionPane.showMessageDialog(null,"You need to go to MRT3,It's the closest.");			
				setVisible(false);
				board.moveToken(8);
			}else if(board.getCurrentPlayer().getCurrentPosition()== 33){
				JOptionPane.showMessageDialog(null,"You need to go to MRT4,It's the closest.");			
				setVisible(false);
				board.moveToken(2);	
			}
		}else if(c.getCommunityRandom()==17){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest receive.jpg")));
			board.getCurrentPlayer().addMoney(20);
			board.setCurrentPlayer();
			board.getPlayerPanelUI().updatePlayerMoney();
		}else if(c.getCommunityRandom()==18){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest receive2.jpg")));
			board.getCurrentPlayer().addMoney(20);
			board.setCurrentPlayer();
			board.getPlayerPanelUI().updatePlayerMoney();
		}else if(c.getCommunityRandom()==19){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest repair1.jpg")));
		//	board.getCurrentPlayer().subtractMoney(20);
			this.chestPayment=20;
			btnPay.setVisible(true);

		}else if(c.getCommunityRandom()==20){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest repair2.jpg")));
//			board.getCurrentPlayer().subtractMoney(20);
			this.chestPayment=20;
			btnPay.setVisible(true);

		}else if(c.getCommunityRandom()==21){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest repair3.jpg")));
//			board.getCurrentPlayer().subtractMoney(20);
			this.chestPayment=20;
			btnPay.setVisible(true);

		}else if(c.getCommunityRandom()==22){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest tax1.jpg")));
//			board.getCurrentPlayer().subtractMoney(50);
			this.chestPayment=50;
			btnPay.setVisible(true);
		}else if(c.getCommunityRandom()==23){
			lblChestCard.setIcon(new ImageIcon(CommunityChestUI.class.getResource("chestpic/community chest tax2.jpg")));
	//		board.getCurrentPlayer().subtractMoney(50);
			this.chestPayment=50;
			btnPay.setVisible(true);
		}/*else if(c.getCommunityRandom()==24){ ///pang test ng JailFreeCard
			lblChestCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs3.jpg")));
			//LIBRENG 'Get out of Jail'
			board.getCurrentPlayer().setHasGetOutOfJailCard(board.currentPlayer.getHasGetOutOfJailCard()+1);
			
		}*/
	}
	
	public void returnGame(){
			board.getDiceUI().rolledDoubles();
			setVisible(false);
	}
	
	public void Pay(){
		if(board.getCurrentPlayer().getMoney()>=chestPayment){
			p.subtractMoney(chestPayment);
			btnPay.setEnabled(false);
	//		btnPay.setVisible(false);
			btnReturn.setEnabled(true);
			btnReturn.setVisible(true);
			board.setCurrentPlayer();
			board.getPlayerPanelUI().updatePlayerMoney();
			}else{
				BankruptcyChecker bc = new BankruptcyChecker(p, bl);
				int worth=bc.checkWorth();
				
				if(worth>=chestPayment){
					JOptionPane.showMessageDialog(null,"You don't have enough Money to pay rent, just mortgage your properties");
				}else{
					bc.transferAssetsBank();
					board.getCurrentPlayer().setActive(false);
					JOptionPane.showMessageDialog(null,"You don't have enough Money to pay rent, Out ka na!");
					btnReturn.setVisible(true);
				}	
				}
		
	}
}
